<?php
/**
 * 前台求助信息管理
 */
defined('IN_PHPCMS') or exit('No permission resources.');
class index{
	private $db;
	public $siteid;
	
	/**
	 * 初始化
	 * Enter description here ...
	 */
	function __construct() {
		$this->db = pc_base::load_model('myhelp_model');
		$this->_userid = param::get_cookie('_userid');
		$this->siteid = get_siteid();
	}
	
	public function init()
	{
		if (isset($_GET['id']) && intval($_GET['id'])) {
			$id = intval($_GET['id']);
			$this->db->update('`hits`=`hits`+1', array('id'=>$id));
			$myhelp = $this->db->get_one(array('id'=>$id));
		}
		$productid = intval($_GET['productid']);
		$content_db = pc_base::load_model('content_model');
		$content_db->set_model(13);
		$product = $content_db->get_one(array('id'=>$productid));
		if ($product) {
			$myhelp_list = $this->db->select(array('productid'=>$productid), 'title,username,modelid,hits,inputtime', '20', 'inputtime DESC');
		} else {
			showmessage('抱歉，您的请求错误！', APP_PATH.'/index.php');
		}
		
		$catid = TOP_PRODUCT_CATID;
		include template('myhelp','index');
	}
	/**
	 * 添加
	 */
	public function add()
	{
		$productid = intval($_POST['id']);
		$title = new_addslashes($_POST['myhelp_title']);
		
		$content_db = pc_base::load_model('content_model');
		$content_db->set_model(13);
		$product = $content_db->get_one(array('id'=>$productid), 'id,title');
		
		$member_db = pc_base::load_model('member_model');
		$member = $member_db->get_one(array('userid'=>$this->_userid), 'userid,username,modelid');

		if ($product && $member) {
			$data = array(
				'productid'		=> $product['id'],
				'product_title'	=> $product['title'],
				'title'			=> $title,
				'userid'		=> $member['userid'],
				'modelid'		=> $member['modelid'],
				'username'		=> $member['username'],
				'inputtime'		=> SYS_TIME
			);
			$data['id'] = $this->db->insert($data, true);
			
			$data['inputtime'] = date('m-d', $data['inputtime']);
			
			//修改产品表最新求助信息创建时间，用于会员中心排序
			$content_db->update(array('myhelp_inputtime'=>date('Y-m-d H:i:s', SYS_TIME)), array('id'=>$product['id']));
			echo json_encode($data);
			exit;
		}
	}
}
